package main

import (
	"fmt"
	"time"
	"math/rand"
)

func generator() chan int{
	out := make(chan int)

	go func() {
		i := 0
		for j := 0; j< 10;j++ {
			time.Sleep(time.Duration(rand.Intn(1500)) * time.Millisecond)
			out <- i
			i++
		}
	}()

	return out
}

func worker(id int, c chan int)  {
	for n := range c{
		fmt.Println("worker:" , n)
	}
}

func createWorker(n int) chan<- int{
	c := make(chan int)
	go worker(n, c)
	return c
}

func main()  {

	var c1, c2 = generator(), generator()

	w := createWorker(0)

	for  {
		select {
		case n := <- c1:
			w <- n
		case n := <- c2:
			w <- n

		}
	}
}
